<template>
  <div>
    <Search :search="search" :reset="reset">
        <template v-slot:body>
            <el-form-item label="功能标识">
                <el-input v-model="query.key" clearable />
            </el-form-item>
            <el-form-item label="描述">
                <el-input v-model="query.description" clearable />
            </el-form-item>
        </template>
    </Search>
    <div class="table-default">
      <Operate :show="open" v-if="isAdmin == 'true'" />
      <el-table :data="tableData" class="mt-3" v-loading="loading" >
<!--        <el-table-column prop="id" label="ID" />-->
          <el-table-column prop="key" label="功能标识" /> 
          <el-table-column prop="value" label="控制" >
              <template #default="scope">
                  <Status v-model="scope.row.value" :id="scope.row.id" :api="api" />
              </template>
          </el-table-column>
        <el-table-column prop="description" label="中文描述" />
        <el-table-column label="操作" width="200" v-if="isAdmin == 'true'">
          <template #default="scope">
            <Update @click="open(scope.row.id)" />
            <Destroy @click="destroy(api, scope.row.id)" />
          </template>
        </el-table-column>
      </el-table>
      <Paginate />
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted , ref } from 'vue'
import Create from './create.vue'
import { useGetList } from '@/composables/curd/useGetList'
import { useDestroy } from '@/composables/curd/useDestroy'
import { useOpen } from '@/composables/curd/useOpen'
import http from '@/support/http'

const api = 'system/system/config'

const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

const isAdmin = ref()
onMounted(() => {
  search() 
  deleted(reset)
  // 判断是否为admin用户
  http.get('user/is_admin').then(res => {
    isAdmin.value = res.data.data
    console.log(isAdmin.value)
  })
})

</script>
